/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package wads.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import wads.com.DocumentTemplateInfo;
import wads.com.WADTools;

public class ReceiveDocumentList extends HttpServlet implements Serializable{
  private static final long serialVersionUID = -5126015519830112566L;
  
  private static final String CONTENT_TYPE = WADTools.SERVLET_CONTENT_TYPE;
  //private static final String SERVERNAME = "localhost";
  private static final String DTINFO_FN = "docTemplate.info";
  /**Initialize global variables*/
  private HttpSession session;

  private File mainPath;
  private byte[] buf = new byte[1024];
  //private File[] listFile;
  //private String fullNamePath;
  private ServletOutputStream sos;
  private String userName;
  private String app_Path;

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }
  
  /**Process the HTTP Get request*/
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    session = request.getSession(false);
    app_Path = WADTools.APP_PATH;
    //change notice: change CAST (string) by use method .toString()
    userName = session.getAttribute("UserName").toString();
    System.out.println(userName);
    sos = response.getOutputStream();

    mainPath = new File(app_Path+"res/docs/activeDocTemplate/");

    try{
      File[] listPath = mainPath.listFiles();
      DocumentTemplateInfo dtInfo;
      sos.print("<html><title>WADS</title><body bgcolor=\"#CCCCFF\">");

      for(int i=0; i<listPath.length; i++) {
        if(listPath[i].isDirectory() && !listPath[i].getName().startsWith(".svn")) {
          String subPath = listPath[i].getName();

          File info = new File(app_Path+"res/docs/activeDocTemplate/" + subPath  +"/"+ DTINFO_FN);
          
          if(info.exists()) {
              ObjectInputStream ois = new ObjectInputStream(new FileInputStream(info));
              dtInfo = (DocumentTemplateInfo)ois.readObject();
              ois.close();

              Properties prop = new Properties();
              prop.load(new FileInputStream(app_Path+"res/wad_app.properties"));
              buf = dtInfo.getTemplate_Name().getBytes();

              sos.println("<a href =getDT?dt_ID="+ dtInfo.getTemplate_ID()+">");
              sos.write(buf, 0, dtInfo.getTemplate_Name().length());
              sos.println("</a><br>");
          }
        }
      }
      sos.println("</body></html>");

    }catch(Exception e){ e.printStackTrace(); }

  }
  /**Clean up resources*/
  @Override
  public void destroy() {
  }
}
